package br.com.cqipac.dao;

// Generated 24/06/2008 08:32:05 by Hibernate Tools 3.2.1.GA

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.com.cqipac.to.Funcionario;
import br.com.cqipac.to.Menu;
import br.com.cqipac.to.NaoConformidade;
import br.com.cqipac.to.Setor;
import br.com.cqipac.to.SetorFuncionario;

/**
 * Home object for domain model class Menu.
 * 
 * @see br.com.cqipac.to.Menu
 * @author Hibernate Tools
 */
@Stateless
public class MenuDao implements RemoteDao {

	private static final Log log = LogFactory.getLog(MenuDao.class);

	@PersistenceContext
	private EntityManager entityManager;

	public void persist(Object transientInstance) throws Exception {
		log.debug("persisting Menu instance");

		entityManager.persist((Menu) transientInstance);
		log.debug("persist successful");

	}

	public void remove(Object persistentInstance) throws Exception {
		log.debug("removing Menu instance");

		entityManager.remove((Menu) persistentInstance);
		log.debug("remove successful");

	}

	public Menu merge(Object detachedInstance) throws Exception {
		log.debug("merging Menu instance");

		Menu result = entityManager.merge((Menu) detachedInstance);
		log.debug("merge successful");
		return result;

	}

	public Menu findById(Integer id) throws Exception {
		log.debug("getting Menu instance with id: " + id);

		Menu instance = entityManager.find(Menu.class, id);
		log.debug("get successful");
		return instance;

	}

	public List<Menu> list() throws Exception {

		log.debug("getting Menu instances list");

		List<Menu> instance = entityManager.createQuery("from Menu menu")
				.getResultList();
		log.debug("get successful");
		return instance;

	}

	@Override
	public Object find(Object searchingInstance, Integer intTipoBusca)
			throws Exception {

		log.debug("getting Menu instance with indRestrito: ");

		String query = "from Menu m where 1=1";

		if (!((SetorFuncionario) searchingInstance).isStsResposta()) {
			query = query + " and m.indRestrito = 'false'";
		}

		if (!((SetorFuncionario) searchingInstance)
				.getFuncionarioByCodFuncionario().isStsAdmin()) {
			query = query + " and m.indAdmin = 'false'";
		}

		List<Menu> instance = entityManager.createQuery(query).getResultList();

		log.debug("get successful");
		return instance;

	}

	@Override
	public List<Funcionario> list(Object searchingInstance,
			Integer intTipoBusca) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
